import { server } from "@/request/index";
import type { IResPageType } from "./api";
export interface skuImageListType {
  id: number;
  spuId: number;
  isDefault: boolean;
  imgUrl: string;
  imgName: string;
}
export interface skuAttrValueListType {
  attrId: number;
  valueId: number;
}
export interface skuSaleAttrValueListType {
  saleAttrId: number;
  saleAttrValueId: number;
}
//添加数据类型
export interface saveSkuInfoType {
  spuId: number;
  // 所属品牌ID
  tmId: number;
  // 所属分类ID
  category3Id: number;

  // SKU名称
  skuName: string;
  // 价格
  price: number;
  // 重量
  weight: number;
  // sku规格描述
  skuDesc: string;
  // sku默认图片
  skuDefaultImg: string;
  skuImageList: skuImageListType[];
  skuAttrValueList: skuAttrValueListType[];
  skuSaleAttrValueList: skuSaleAttrValueListType[];
}
//获取spu详情数据类型
export interface findBySpuIdType {
  id: number;
  spuId: number;
  price: number;
  skuName: string;
  skuDesc: string;
  weight: string;
  tmId: number;
  category3Id: number;
  skuDefaultImg: string;
  isSale: number;
  createTime: string;
  skuImageList: null;
  skuAttrValueList: null;
  skuSaleAttrValueList: null;
}
export interface newSkuImageListType extends skuImageListType {
  spuImgId: null;
}
export interface newSkuAttrValueList extends skuAttrValueListType {
  id: number;
  skuId: number;
  attrName: number;
  valueName: number;
}
export interface newSkuSaleAttrValueList extends skuSaleAttrValueListType {
  id: number;
  skuId: number;
  spuId: number;
  saleAttrName: string;
  saleAttrValueName: string;
}
//增改数据
export type newFindBySpuIdType = Omit<
  findBySpuIdType,
  "skuImageList" | "skuAttrValueList" | "skuSaleAttrValueList"
> & {
  skuImageList: newSkuImageListType[];
  skuAttrValueList: newSkuAttrValueList[];
  skuSaleAttrValueList: newSkuSaleAttrValueList[];
};

export const reqSaveSkuInfo = (data: saveSkuInfoType) => {
  return server.post<any, null>(`/product/saveSkuInfo`, data);
};
//
//获取详情
export const reqfindBySpuId = (spuId: number) => {
  return server.get<any, findBySpuIdType[]>(`/product/findBySpuId/${spuId}`);
};
//获取分页列表
export const reqSkuList = (page: number = 1, limit: number = 5) => {
  return server.get<any, IResPageType<findBySpuIdType>>(
    `/product/list/${page}/${limit}`
  );
};
//上下架
export const reqOnSale = (id: number) => {
  return server.get(`/product/onSale/${id}`);
};
// 下架
export const reqCancelSale = (id: number) => {
  return server.get(`/product/cancelSale/${id}`);
};
//删除
export const reqDelete = (skuId: number) => {
  return server.delete(`/product/deleteSku/${skuId}`);
};
//查看sku详情
export const reqSkuDetail = (skuId: number) => {
  return server.get<any, newFindBySpuIdType>(`product/getSkuById/${skuId}`);
};
